﻿using Goooodbye.Core.Services;
using DocumentFormat.OpenXml.Packaging;
using System;
using System.Data;
using System.Windows.Forms;

namespace Goooodbye.Presentation.WinFormsApp
{
    public partial class FormSimplifiedCheck : FormBase
    {
        private DataTable dtProofing = new DataTable();
        private IDictService dictService;

        public FormSimplifiedCheck(IDictService dictService)
        {
            InitializeComponent();
            this.dictService = dictService;

            this.richTextBox1.AllowDrop = true;
            this.richTextBox1.DragDrop += RichTextBox1_DragDrop;
        }

        private void FormSimplifiedCheck_Load(object sender, EventArgs e)
        {
            this.dgvProofing.AutoGenerateColumns = false;
            dtProofing.Columns.Add("ColTerm", typeof(String));
            dtProofing.Columns.Add("ColMapping", typeof(String));
            dtProofing.Columns.Add("ColNote", typeof(String));
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            try
            {
                this.richTextBox1.ReadOnly = true;
                dtProofing.Clear();
                if (this.richTextBox1.Text.Length > 0)
                {
                    var items = dictService.Query().Items;
                    foreach (var item in items)
                    {
                        if (this.richTextBox1.Text.Contains(item.Term))
                        {
                            var row = dtProofing.NewRow();
                            row["ColTerm"] = item.Term;
                            row["ColMapping"] = item.Mapping;
                            row["ColNote"] = item.Note;
                            dtProofing.Rows.Add(row);
                        }
                    }
                }
                this.dgvProofing.DataSource = dtProofing;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.groupBoxProofing.Text = $"校对 ({this.dgvProofing.Rows.Count})";
                this.richTextBox1.ReadOnly = false;
            }
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Open(this.openFileDialog1.FileName);
            }
        }

        private void RichTextBox1_DragDrop(object sender, DragEventArgs e)
        {
            var fileName = ((Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            if (fileName.EndsWith(".docx"))
            {
                Open(fileName);
            }
        }

        private void Open(string fileName)
        {
            try
            {
                this.richTextBox1.Clear();
                using (var document = WordprocessingDocument.Open(fileName, false))
                {
                    var body = document.MainDocumentPart.Document.Body;
                    foreach (var element in body.ChildElements)
                    {
                        this.richTextBox1.AppendText($"{element.InnerText}{Environment.NewLine}");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
